using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
//using Microsoft.Build.BuildEngine;
using System.Collections.Specialized;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Xml;
using schemas.microsoft.com.developer.msbuild.Item2003;

namespace SetCommonConfigOnTargets
{
	public interface Filter
	{
		bool Match(FileInfo file);
		void Process(FileInfo file);
	}
	class Program
	{

		static void Main(string[] args)
		{
			DirectoryInfo root = new DirectoryInfo("../../../");
			List<Filter> filters = new List<Filter>();
			filters.Add(new CommonTargetsFilter());
			filters.Add(new TestProjectFilter()
			{
				BasePath = @"..\..\..\bin"
			});
			filters.Add(new AssemblyInfoFixFilter());
			Set(root, filters);
		}

		private static void Set(DirectoryInfo root, List<Filter> filters)
		{
			foreach(var file in root.GetFiles())
			{
				foreach(var filter in filters)
				{
					if(filter.Match(file))
						filter.Process(file);
				}
			}
			foreach(var dir in root.GetDirectories())
			{
				Set(dir, filters);
			}
		}
	}


	public class PathUtil
	{
		/// <summary>
		/// Creates a relative path from one file
		/// or folder to another.
		/// </summary>
		/// <param name="fromDirectory">
		/// Contains the directory that defines the
		/// start of the relative path.
		/// </param>
		/// <param name="toPath">
		/// Contains the path that defines the
		/// endpoint of the relative path.
		/// </param>
		/// <returns>
		/// The relative path from the start
		/// directory to the end path.
		/// </returns>
		/// <exception cref="ArgumentNullException"></exception>
		public static string RelativePathTo(
			string fromDirectory, string toPath)
		{
			if(fromDirectory == null)
				throw new ArgumentNullException("fromDirectory");

			if(toPath == null)
				throw new ArgumentNullException("toPath");

			bool isRooted = Path.IsPathRooted(fromDirectory)
				&& Path.IsPathRooted(toPath);

			if(isRooted)
			{
				bool isDifferentRoot = string.Compare(
					Path.GetPathRoot(fromDirectory),
					Path.GetPathRoot(toPath), true) != 0;

				if(isDifferentRoot)
					return toPath;
			}

			StringCollection relativePath = new StringCollection();
			string[] fromDirectories = fromDirectory.Split(
				Path.DirectorySeparatorChar);

			string[] toDirectories = toPath.Split(
				Path.DirectorySeparatorChar);

			int length = Math.Min(
				fromDirectories.Length,
				toDirectories.Length);

			int lastCommonRoot = -1;

			// find common root
			for(int x = 0; x < length; x++)
			{
				if(string.Compare(fromDirectories[x],
					toDirectories[x], true) != 0)
					break;

				lastCommonRoot = x;
			}
			if(lastCommonRoot == -1)
				return toPath;

			// add relative folders in from path
			for(int x = lastCommonRoot + 1; x < fromDirectories.Length; x++)
				if(fromDirectories[x].Length > 0)
					relativePath.Add("..");

			// add to folders to path
			for(int x = lastCommonRoot + 1; x < toDirectories.Length; x++)
				relativePath.Add(toDirectories[x]);

			// create relative path
			string[] relativeParts = new string[relativePath.Count];
			relativePath.CopyTo(relativeParts, 0);

			string newPath = string.Join(
				Path.DirectorySeparatorChar.ToString(),
				relativeParts);

			return newPath;
		}

	}


}
